package com.jspm.config;

import javax.sql.DataSource;

import com.baomidou.mybatisplus.MybatisConfiguration;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@Configuration
//@EnableTransactionManagement // 启用事务管理
//@MapperScan("com.dao")  // 修改为你的实际DAO包路径
public class MybatisPlusConfig {

    @Autowired
    private DataSource dataSource;

    /**
     * MyBatis-Plus配置
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);

        // 配置Mapper XML路径
        sqlSessionFactory.setMapperLocations(
                new PathMatchingResourcePatternResolver()
                        .getResources("classpath:mapper/*.xml"));

        // MyBatis配置
//        sqlSessionFactory.setConfigLocation(
//                new PathMatchingResourcePatternResolver()
//                        .getResource("classpath:mybatis/mybatis-config.xml"));

        // 实体类包路径
        sqlSessionFactory.setTypeAliasesPackage("com.jspm.entity.*");
        sqlSessionFactory.setTypeEnumsPackage("com.jspm.model.enums");

        // 插件配置
        Interceptor[] plugins = { paginationInterceptor() };
        sqlSessionFactory.setPlugins(plugins);

        // 全局配置
        sqlSessionFactory.setGlobalConfig(globalConfig());

        // 原生Mybatis配置
        sqlSessionFactory.setConfiguration(configuration());

        return sqlSessionFactory.getObject();
    }

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        page.setDialectType("mysql"); // 指定数据库类型
        return page;
    }

    /**
     * 全局配置
     */
    @Bean
    public GlobalConfiguration globalConfig() {
        GlobalConfiguration globalConfig = new GlobalConfiguration();
        // ID类型: 2表示全局唯一ID
        globalConfig.setIdType(2);
        // 元对象处理器
        globalConfig.setMetaObjectHandler(new MyMetaObjectHandler());
        return globalConfig;
    }

    /**
     * 创建Mybatis原生配置
     */
    @Bean
    public MybatisConfiguration configuration() {
        // 创建Mybatis原生配置
        MybatisConfiguration configuration = new MybatisConfiguration();

        // 设置日志实现
        configuration.setLogImpl(StdOutImpl.class);

        // 设置是否在值为null时调用setter方法
        configuration.setCallSettersOnNulls(false);

        // 开启下划线转驼峰
        configuration.setMapUnderscoreToCamelCase(true);

        // 对应XML中的各项配置
        configuration.setCacheEnabled(true);                // cacheEnabled
        configuration.setLazyLoadingEnabled(true);          // lazyLoadingEnabled
        configuration.setMultipleResultSetsEnabled(true);   // multipleResultSetsEnabled
        configuration.setUseColumnLabel(true);              // useColumnLabel
        configuration.setDefaultExecutorType(ExecutorType.REUSE); // defaultExecutorType
        configuration.setDefaultStatementTimeout(25000);    // defaultStatementTimeout
        configuration.setMapUnderscoreToCamelCase(true);    // mapUnderscoreToCamelCase

        return configuration;
    }

    /**
     * 事务管理器
     */
    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}